package vworld.business.avatar;

import generated.business.avatar.AvatarServicePOA;
import generated.business.avatar.AvatarServicePackage.Gender;
import generated.business.avatar.AvatarServicePackage.Mood;
import generated.business.avatar.AvatarServicePackage.Size;
import generated.callback.ClientCallback;
import generated.domain.avatar.Avatar;
import generated.domain.user.User;

import java.util.Map;

import org.omg.CORBA.ORB;

import vworld.ServerSingleton;
import vworld.adapter.GenderAdapter;
import vworld.adapter.MoodAdapter;
import vworld.adapter.SizeAdapter;

public class AvatarServiceServant extends AvatarServicePOA {

	private ORB orb;

	public void setOrb(ORB pOrb) {
		orb = pOrb;
	}

	public Avatar createAvatar(User pUser, String pName, Mood pMood,
			Gender pGender, Size pSize) {
		pUser.setAvatar(null);
		ServerSingleton.getInstance().USER_DAO.saveOrUpdateUser(pUser);

		Avatar avatar = new Avatar();
		avatar.setName(pName);
		avatar.setGender(GenderAdapter.toString(pGender));
		avatar.setMood(MoodAdapter.toString(pMood));
		avatar.setSize(SizeAdapter.toString(pSize));
		avatar.setState(false);
		pUser.setAvatar(avatar);

		ServerSingleton.getInstance().USER_DAO.saveOrUpdateUser(pUser);

		updateClientCallbackRef(pUser);

		return avatar;
	}

	private void updateClientCallbackRef(User user) {
		ClientCallback cl = ServerSingleton.getInstance().REGISTERED_CLIENT_MAP
				.get(user.getLogin());

		if (cl != null) {
			cl.avatarName(user.getAvatar().getName());
			ServerSingleton.getInstance().REGISTERED_CLIENT_MAP.put(user
					.getLogin(), cl);
		}
	}

	public void changeMood(ClientCallback client, User user, Mood mood) {
		user.getAvatar().setMood(MoodAdapter.toString(mood));
		ServerSingleton.getInstance().USER_DAO.saveOrUpdateUser(user);

		String message = user.getAvatar().getName() + " is now "
				+ user.getAvatar().getMood();

		for (Map.Entry<String, ClientCallback> e : ServerSingleton
				.getInstance().REGISTERED_CLIENT_MAP.entrySet()) {
			if (e.getValue().roomName().equals(client.roomName()))
				e.getValue().doNotifyOnMoodChange(e.getValue().avatarName(),
						message);
		}

		// update admin's worlds view
		for (ClientCallback cl : ServerSingleton.getInstance()
				.getAdminCallBackList()) {
			cl.updateWorldsView(client.roomName());
		}
	}

	public boolean existAvatar(String avatarName) {
		Avatar a = ServerSingleton.getInstance().AVATAR_DAO
				.getAvatarByName(avatarName);
		if (a == null) {
			return false;
		} else {
			return true;
		}
	}

	public String getUserRoleByAvatarName(String avatarName) {
		return ServerSingleton.getInstance().AVATAR_DAO
				.getUserRolebyAvatarName(avatarName);
	}

	public String getRoomNameByAvatarName(String avatarName) {
		return ServerSingleton.getInstance().AVATAR_DAO
				.getRoomNameByAvatarName(avatarName);
	}

	public User getUser(Avatar avatar) {
		return ServerSingleton.getInstance().AVATAR_DAO
				.getUserByAvatarName(avatar.getName());
	}
}
